<template>
  <base-breadcrumb v-loading="loading.isLoad">
    <a-card class="mb20">
      <titleName :title="form.meetingName" />
      <a-form-model ref="form" :model="form" :rules="rules" :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }">
        <a-row :gutter="24">
<!--          <a-col :md="8" :sm="8">-->
<!--            <a-form-model-item label="分公司：">-->
<!--              <BaseInput v-model="form.branchComName" disabled placeholder="加载中" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :md="8" :sm="8">-->
<!--            <a-form-model-item label="项目名称：">-->
<!--              <BaseInput v-model="form.projectName" disabled placeholder="加载中" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="8">
            <a-form-model-item label="所属单位：">
            <org-tree-select disabled :value="form.projectId"></org-tree-select>
            </a-form-model-item>
          </a-col>
          <a-col v-if="form.meetingTypeCategory == 4" :md="8" :sm="8">
            <a-form-model-item label="月份：">
              <a-month-picker v-model="form.month" placeholder="加载中" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :md="8" :sm="8">
            <a-form-model-item label="监理会议期数：">
              <BaseInput v-model="form.meetingName" disabled placeholder="加载中" />
            </a-form-model-item>
          </a-col>
          <a-col :md="8" :sm="8" >
            <a-form-model-item label="是否具备召开会议条件">
              <a-select v-model="form.isHaveMeetCondition" placeholder="请选择" disabled >
                <a-select-option :value="1" >是</a-select-option>
                <a-select-option :value="0" >否</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :md="24" :sm="24" v-if="form.isHaveMeetCondition === 0" >
            <a-form-model-item label="未召开会议原因：" >
              <BaseTextarea v-model="form.notMeetReason" placeholder="加载中" disabled/>
            </a-form-model-item>
          </a-col>
          <div v-if="form.isHaveMeetCondition === 1">
            <a-col :md="8" :sm="8">
              <a-form-model-item label="主持人/主讲人:">
                <BaseInput v-model="form.speaker" disabled placeholder="加载中" />
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item label="会议地点：">
                <BaseInput v-model="form.meetingPlace" disabled placeholder="加载中" />
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item label="召开时间：">
                <BaseInput v-model="form.startTime" disabled placeholder="加载中" />
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item label="记录人：">
                <BaseInput v-model="form.creatorName" disabled placeholder="加载中" />
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item label="记录时间：">
                <BaseInput v-model="form.createTime" disabled placeholder="加载中" />
              </a-form-model-item>
            </a-col>
            <a-col v-if="form.meetingTypeCategory == 7" :md="8" :sm="8">
              <a-form-model-item label="交底方案：" prop="commentedPlan">
                <BaseInput v-model="form.commentedPlan" disabled placeholder="加载中" />
              </a-form-model-item>
            </a-col>
            <a-col
              v-if="form.meetingTypeCategory == 7 || form.meetingTypeCategory == 8"
              :md="24"
              :sm="24"
              key="meetingExplains"
            >
              <a-form-model-item label="会议说明：" prop="meetingExplains">
                <BaseTextarea v-model.trim="form.meetingExplains" disabled placeholder="加载中" />
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item label="参会人数：" prop="meetingCount">
                <BaseInputNumber v-model.trim="form.meetingCount" :min="0" disabled placeholder="请输入" />
              </a-form-model-item>
            </a-col>
            <a-col v-if="form.meetingTypeCategory == 8 || form.meetingTypeCategory == 9" :md="24" :sm="24">
              <a-form-model-item label="主要参会人员：" prop="mainParticipants">
                <BaseTextarea v-model.trim="form.mainParticipants" disabled placeholder="加载中      " />
              </a-form-model-item>
            </a-col>
            <a-col v-if="form.meetingTypeCategory == 5" :md="8" :sm="8">
              <a-form-model-item label="教育课时：" prop="educationHour">
                <BaseInput v-model="form.educationHour" disabled placeholder="加载中" />
              </a-form-model-item>
            </a-col>
            <a-col v-if="form.meetingTypeCategory == 5" :md="8" :sm="8">
              <a-form-model-item label="教育负责人：" prop="educationResponsible">
                <BaseInput v-model="form.educationResponsible" disabled placeholder="加载中" />
              </a-form-model-item>
            </a-col>
            <a-col v-if="form.meetingTypeCategory == 5" :md="24" :sm="24">
              <a-form-model-item label="受教育单位：" prop="educationOrganization">
                <BaseTextarea v-model="form.educationOrganization" disabled placeholder="请输入" />
              </a-form-model-item>
            </a-col>
            <a-col v-if="form.meetingTypeCategory !== 7" :md="24" :sm="24">
              <a-form-model-item label="会议纪要：" prop="meetingSummary">
                <!-- <editor-bar
                                  v-if="editor"
                                  :disabled="pageType != 'edit'"
                                  @change="commentsVal"
                                  :maxLength="30000"
                                  v-model="form.meetingSummary"
                                  :isClear="isClear"
                                  style="width: 100%"
                              ></editor-bar> -->
                <BaseTextarea
                  v-model="form.meetingSummary"
                  :auto-size="{ minRows: 20, maxRows: 25 }"
                  :max-length="30000"
                  :disabled="pageType != 'edit'"
                  placeholder="请输入"
                />
                <!-- <div
                  v-else
                  style="max-height: 300px; overflow: auto; background-color: #f5f5f5"
                  v-html="form.meetingSummary"
                /> -->
              </a-form-model-item>
            </a-col>
          </div>
        </a-row>
      </a-form-model>
    </a-card>
    <div v-if="form.isHaveMeetCondition === 1">
      <div v-if="form.meetingTypeCategory == 7">
        <a-card class="mb20" v-if="pageType !== 'start'">
          <titleName title="会议纪要" />
          <a-form-model ref="form7" :model="form" :rules="rules" :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }">
            <a-row :gutter="24">
              <a-col :md="24" :sm="24">
                <a-form-model-item label="主要内容：" prop="meetingSummary">
                  <BaseTextarea
                    v-if="pageType !== 'look'"
                    v-model="form.meetingSummary"
                    :auto-size="{ minRows: 20, maxRows: 25 }"
                    :max-length="30000"
                    :disabled="pageType != 'edit'"
                    placeholder="请输入"
                  />
                  <div
                    v-else
                    style="max-height: 300px; overflow: auto; background-color: #f5f5f5"
                    v-html="form.meetingSummary"
                  />
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="24">
              <a-col :md="24" :sm="24">
                <a-form-model-item label="附件上传：">
                  <upload-list
                    :businessCode="'SHE04'"
                    :uploadArrys.sync="form.fileList"
                    @fileArrysData="handleFileUploaded"
                    :disabled="pageType != 'edit'"
                    multiple
                  ></upload-list>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </a-card>
      </div>

      <a-card class="mb20" v-else>
        <titleName title="相关资料上传" />
        <a-form-model ref="ruleForm" :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }">
          <div class="table-operator row">
            <!-- <l-table
              upload-type="normal"
              text="新增"
              btn-type="primary"
              :upload-arrys="form.fileList"
              business-code="SHE04"
              @fileArrysData="fileArrysData"
            /> -->
            <a-row :gutter="24">
              <a-col span="24">
                <a-form-model-item label="会议纪要" prop="summaryFileList">
                  <l-table
                    text="上传附件"
                    btn-type="primary"
                    :upload-arrys.sync="form.summaryFileList"
                    business-code="SHE04"
                    :needChineseName="true"
                    errorTips="附件名称中必须含有中文"
                    :isClose="false"
                  />
                </a-form-model-item>
              </a-col>
              <a-col span="24">
                <a-form-model-item label="会议纪要整改回复" prop="summaryReplyFileList">
                  <l-table
                    text="上传附件"
                    btn-type="primary"
                    :upload-arrys.sync="form.summaryReplyFileList"
                    business-code="SHE04"
                    :needChineseName="true"
                    errorTips="附件名称中必须含有中文"
                    :isClose="false"
                  />
                </a-form-model-item>
              </a-col>
              <a-col span="24">
                <a-form-model-item label="其他附件" prop="fileList">
                  <l-table
                    text="上传附件"
                    btn-type="primary"
                    :upload-arrys.sync="form.fileList"
                    business-code="SHE04"
                    :needChineseName="true"
                    errorTips="附件名称中必须含有中文"
                    :isClose="false"
                  />
                </a-form-model-item>
              </a-col>
            </a-row>
          </div>
          <!-- <s-table
            ref="table"
            bordered
            size="default"
            row-key="key"
            :columns="columns"
            :data="loadData"
            :alert="true"
            show-size-changer
            show-quick-jumper
            :show-pagination="false"
          >
            <span slot="serial" slot-scope="text, record, index">{{ index + 1 }}</span>
            <span slot="action" slot-scope="text, record, index">
              <a :href="'/api/ycloud-file-center/oper/download?access_token=' + token + '&fileId=' + record.fileId"
                >下载</a
              >
              <a-divider type="vertical" />
              <a href="javascript:" @click="$filePreview(record)">查看</a>
              <a-divider v-if="pageType != 'look' && pageType != 'start'" type="vertical" />
              <a v-if="pageType != 'look' && pageType != 'start'" @click="delItem(index)">删除</a>
            </span>
          </s-table> -->
        </a-form-model>
      </a-card>
      <seeSign :id="id" :visible="signInVsible" @cancel="signInVsible = false" @confirm="signInVsible = false" />
    </div>
    <div v-if="pageType !== 'look'" class="tool-btns page-btn-right-top">
      <!-- <a-button
                v-if="id != '' && formInfo.auditStatus == 'DRAFT'"
                type="danger"
                :loading="loading.DELETE"
                @click="handleSave('DELETE')"
                >删除草稿</a-button
            > -->
      <!-- <a-button
                v-if="pageType != 'start'"
                type="primary"
                :disabled="form.meetingSummary == ''"
                :loading="loading.DRAFT"
                @click="handleSave('DRAFT')"
                >保存草稿</a-button
            > -->
      <a-button
        v-if="pageType == 'start' && currentUer"
        type="primary"
        :loading="loading.START"
        @click="handleSave('START')"
      >
        开始会议
      </a-button>
      <a-button
        v-if="pageType != 'start' && currentUer"
        type="primary"
        :loading="loading.SUBMITTED"
        @click="handleSave('SUBMITTED')"
      >
        提交
      </a-button>
      <a-button type="primary" @click="handleClose"> 关闭 </a-button>
    </div>
    <div v-else class="tool-btns">
      <BasePrintButton
        v-if="form.meetingTypeCategory == '5' || form.meetingTypeCategory == '6' || form.meetingTypeCategory == '7'"
        type="primary"
        :before-print-fn="getMeetingPrintReq"
      />
      <div class="tool-btns page-btn-right-top">
      <a-button type="primary" class="ml20" @click="handleClose"> 关闭 </a-button>
      </div>
    </div>
  </base-breadcrumb>
</template>
<script>
import { STable } from '@/components'
import UploadList from '@/components/UploadList'

import { getMeeting, meetingSummary, startMeeting, printMeetingDetail } from '@/api/office/conference'
import seeSign from '@/components/seeSign'
import { getMeetingPrint } from '@/api/safetySupervision/metting'
import BasePrintButton from '@/components/BasePrintButton' // 打印按钮
const columns = [
  {
    title: '序号',
    dataIndex: 'serial',
    width: '5%',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '文件名称',
    dataIndex: 'fileName',
    width: '40%'
  },
  {
    title: '文件格式',
    dataIndex: 'suffixName'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '15%',
    scopedSlots: { customRender: 'action' }
  }
]

import Vue from 'vue'
export default {
  name: 'inputSummaryNew',
  components: {
    STable,
    seeSign,
    BasePrintButton,
    UploadList
  },
  data() {
    return {
      signInVsible: false,
      loading: {
        DELETE: false,
        DRAFT: false,
        SUBMITTED: false,
        START: false,
        isLoad: false
      },
      columns,
      isClear: false,
      form: { fileList: [], meetingSummary: '' },
      visible: false,
      rules: {
        meetingSummary: [{ required: true, message: '请填写会议纪要', trigger: 'change' }]
      },
      id: '',
      pageType: '',
      token: Vue.getAcloudProvider().getAccessToken(),
      loadData: () => {
        return new Promise((resolve, reject) => {
          resolve({
            data: this.form.fileList
          })
        })
      },
      editor: true
    }
  },
  watch: {
    $route: {
      handler(to, from) {
        if (this.id !== this.$route.query.id) {
          this.initPage()
        }
      }
    }
  },
  computed: {
    currentUer() {
      const userInfo = Vue.getAcloudProvider().getUserInfo()
      console.log(userInfo.name == this.form.creatorName)
      return userInfo.name == this.form.creatorName
    }
  },
  mounted() {
    this.initPage()
  },
  methods: {
    initPage() {
      if (this.$route.query.id) {
        this.id = this.$route.query.id
        this.pageType = this.$route.query.pageType
        this.initTitle()
        this.getMeeting()
      }
    },
    initTitle() {
      if (this.pageType == 'edit') {
        this.$route.meta.title = '填写会议纪要'
      } else {
        this.$route.meta.title = '会议详情'
      }
    },
    commentsVal: function (val) {
      console.log(val)
      this.form.meetingSummary = val
    },
    validateForm() {
      let flag = false
      this.$refs.form.validate((valid) => {
        flag = valid
        if(!valid) {
          this.$message.warning("请填写完所有必填项信息")
        }
      })
      if (this.form.meetingTypeCategory == 7) {
        this.$refs.form7.validate((valid) => {
          flag = valid
          if(!valid) {
            this.$message.warning("请填写完所有必填项信息")
          }
        })
      }
      console.log(flag)
      return flag
    },
    // 处理增、删、改操作
    async handleSave(status) {
      if (this.pageType == 'start') {
        this.loading[status] = true
        const [err, res] = await startMeeting({ id: this.id })
        this.loading[status] = false
        this.editor = false
        this.$nextTick(() => {
          this.pageType = 'edit'
          this.editor = true
        })

        this.getMeeting()
        this.initTitle()
        return
      }
      if (!this.validateForm()) {
          this.$message.warning("请填写完所有必填项信息")
        return
      }
      if (this.loading[status]) return
      this.loading[status] = true
      const params = {
        ...this.form,
        status: status == 'DRAFT' ? 10 : 20
      }
      // const { pageType } = this.$route.query
      // const fn = pageType === 'edit' ? meetingSummary : meetingSummary
      // const map = {
      //     DRAFT: meetingSummary,
      //     SUBMITTED: meetingSummary,
      // }
      const [err, res] = await meetingSummary(params)
      if (!err) {
        this.$message.success('成功')
        this.handleClose()
      }
      this.loading[status] = false
    },
    getMeetingPrintReq() {
      if (this.form.meetingTypeCategory == '7') {
        return printMeetingDetail({ id: this.id }).then((res) => {
          return res[1].data.content
        })
      } else {
        return getMeetingPrint({
          id: this.id,
          type: this.$route.query.printType === 'education' ? 'AQJYJJ' : 'AQGJ'
        }).then((res) => {
          return res[1].data.content
        })
      }
    },
    async getMeeting() {
      this.loading.isLoad = true
      const [err, res] = await getMeeting({ id: this.id })
      if (!err) {
        this.form = res.data
        if(this.form.isHaveMeetCondition!==0&&this.form.isHaveMeetCondition!==1)this.form.isHaveMeetCondition = 1
        this.$refs.table && this.$refs.table.refresh && this.$refs.table.refresh(true)
      }
      this.loading.isLoad = false
    },
    delItem(idx) {
      this.form.fileList.splice(idx, 1)
      this.$refs.table.refresh(true)
      console.log(this.form.fileList)
    },
    fileArrysData: function (val) {
      this.form.fileList = val
      this.$refs.table.refresh(true)
    },
    handleFileUploaded(list) {
      this.form.fileList = [...list]
    },
    onSearch() {
      this.visible = true
    },
    handleClose() {
      this.$multiTab.close(this.$route.fullpath)
    },
    onSelectChange(e) {
      console.log('selectedRowKeys changed: ', e)
    },
    handlePage() {
      let routeData = this.$router.resolve({
        path: '/conference/standardConference/conferenceList/conferenceScreen',
        query: {
          id: this.id
        }
      })
      window.open(routeData.href, '_blank')
    }
  }
}
</script>
<style lang="less" scoped>
@deep: ~'>>>';
.tool-btns {
  display: flex;
  justify-content: flex-end;
  @{deep} .ant-btn + .ant-btn {
    margin-left: 12px;
  }
}
.flex {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  height: 40px;
}
</style>
